package com.demo.web.common;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.utils.JavaSmsApi;
import com.demo.web.CaptchaController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by byte2 on 2017-03-30.
 */
@Controller
public class Code {
    private Logger logger = LoggerFactory.getLogger(CaptchaController.class);

    /**
     * 短信验证码
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "phone-code")
    public BaseResult<Object> getSmsCode(HttpServletRequest request, String phone) {

        String capText = Constants.generateSmsCode();
        request.getSession().setAttribute(Constants.PHONE_CODE, capText);
        try {
            return new BaseResult<Object>(true, JavaSmsApi.sender(capText, phone));
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("发送短信验证码异常: " + capText);
        }
        return new BaseResult(true, "success",200);
    }

}
